package com.spring.springboot;

import com.spring.springboot.sys.core.constant.BusinessConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;

import static com.spring.springboot.sys.core.constant.FieldConstants.*;

/**
 * @author 15625
 */
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
@EnableTransactionManagement
public class MicroServiceApp extends SpringBootServletInitializer {

    private static final Logger logger = LoggerFactory.getLogger(MicroServiceApp.class);

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableEnvironment environment = SpringApplication.run(MicroServiceApp.class, args).getEnvironment();
        String property = environment.getProperty(KNIFE4J_PRODUCTION);
        if (StringUtils.isNotBlank(property) && Objects.equals(BusinessConstant.Boolean.FALSE, property)) {
            StringBuilder builder = new StringBuilder();
            builder.append(FIELD_HTTP).append(InetAddress.getLocalHost().getHostAddress()).append(COLON).append(environment.getProperty(SERVER_PORT)).append(SWAGGER_THEME);
            logger.info("====启动完成,接口测试请访问:{}", builder);
        }
    }

    /**
     * 打war包-----> 启动类继承SpringBootServletInitializer类、实现configure方法
     *
     * @param builder 构建器
     * @return SpringApplicationBuilder
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MicroServiceApp.class);
    }

}
